<%@page import="java.util.StringTokenizer"%>
<%@page import="java.sql.DriverManager"%>
<%@page import="Configurations.MySQL"%>
<%@page import="java.sql.PreparedStatement"%>
<%@page import="java.sql.ResultSet"%>
<%@page import="java.sql.Connection"%>
<%
    session.setAttribute("displayPage", "profile");
    // Redirect if this page is being displayed by itself
    if (request.getServletPath().equals("/profile.jsp")) {
        System.out.println("Current Page is set to profile. Redirect to index.jsp");
        response.sendRedirect("");
    }
%>
<h3>Profile</h3>
<%
    if (session.getAttribute("profileChange") != null) {
%>
<div id="content"><h4>Profile changes have been saved</h4></div>
<%
        session.setAttribute("profileChange", null);
    }
    Connection con = null;
    ResultSet rs = null;
    PreparedStatement ps = null;
    try {
        Class.forName(MySQL.DB_DRIVER);
        con = DriverManager.getConnection(MySQL.DB_URL, MySQL.DB_USER, MySQL.DB_PASS);
        String sql = "SELECT * FROM " + MySQL.DB_LOGIN + " WHERE " + MySQL.DB_LOGIN_USERNAME + "=?";
        ps = con.prepareStatement(sql);
        ps.setString(1, session.getAttribute(MySQL.DB_LOGIN_USERNAME).toString());
        rs = ps.executeQuery();

        System.out.println("Query for profile information of the current user: Successful");
        if (rs.next()) {
            System.out.println("Displaying user profile in html form.");
%>
<form action="saveProfile.jsp" method="post">
    <table class="uploadTable">
        <tr>
            <td>
                <strong>Username:</strong>
            </td>
            <td>
                <% out.println(rs.getString(MySQL.DB_LOGIN_USERNAME));%>
            </td>
        </tr>
        <tr>
            <td>
                <strong>Email:</strong>
            </td>
            <td>
                <% String email = rs.getString(MySQL.DB_LOGIN_EMAIL);%>
                <input type="text" name="email" <% out.println("value=\"" + email + "\"");%> size="20"/>
            </td>
        </tr>
        <tr>
            <td>
                <strong>Countries of Expertise:</strong>
            </td>
            <td>
                <% String country = rs.getString(MySQL.DB_LOGIN_CNTRY);%>
                <input type="text" name="country" <% out.println("value=\"" + country + "\"");%> size="20"/>
            </td>
        </tr>
        <tr>
            <td>
                <strong>Topics of Expertise:</strong>
            </td>
            <td>
                <%
                    StringTokenizer tokenizer = new StringTokenizer(rs.getString(MySQL.DB_LOGIN_RSRCH), ",");
                    boolean ai = false;
                    boolean gav = false;
                    boolean networks = false;
                    boolean plv = false;
                    boolean security = false;
                    boolean systems = false;
                    boolean theory = false;
                    boolean vnpl = false;
                    while (tokenizer.hasMoreTokens()) {
                        String token = tokenizer.nextToken();
                        if (token.equals("ai")) {
                            ai = true;
                        } else if (token.equals("gav")) {
                            gav = true;
                        } else if (token.equals("networks")) {
                            networks = true;
                        } else if (token.equals("plv")) {
                            plv = true;
                        } else if (token.equals("security")) {
                            security = true;
                        } else if (token.equals("systems")) {
                            systems = true;
                        } else if (token.equals("theory")) {
                            theory = true;
                        } else if (token.equals("vnpl")) {
                            vnpl = true;
                        }
                    }
                %>
                <input type="checkbox" name="research1" value="ai" <% if (ai) {
                        out.println("checked");
                    }%>/> Artificial Intelligence<br/>
                <input type="checkbox" name="research2" value="gav" <% if (gav) {
                        out.println("checked");
                    }%>/> Graphics and Visualization<br/>
                <input type="checkbox" name="research3" value="networks" <% if (networks) {
                        out.println("checked");
                    }%>/> Networks <br/>
                <input type="checkbox" name="research4" value="plv" <% if (plv) {
                        out.println("checked");
                    }%>/> Programming Languages / Verification<br/>
                <input type="checkbox" name="research5" value="security" <% if (security) {
                        out.println("checked");
                    }%>/> Security<br/>
                <input type="checkbox" name="research6" value="systems" <% if (systems) {
                        out.println("checked");
                    }%>/> Systems<br/>
                <input type="checkbox" name="research7" value="theory" <% if (theory) {
                        out.println("checked");
                    }%>/> Theory<br/>
                <input type="checkbox" name="research8" value="vnpl" <% if (vnpl) {
                        out.println("checked");
                    }%>/> Vision / Natural Language Processing<br/>
            </td>
        </tr>
        <tr>
            <td>
                <strong>Number of Reviews Assigned This Year:</strong>
            </td>
            <td>
                <% out.println(rs.getInt(MySQL.DB_LOGIN_NUMREVIEWS));%>
            </td>
        </tr>
        <tr>
            <td>
                <strong>Available:</strong>
            </td>
            <td>
                <%
                    boolean available = false;
                    if (rs.getInt(MySQL.DB_LOGIN_AVAILABLE) > 0) {
                        available = true;
                    }
                %>
                <input type="radio" name="available" value="1" <% if (available) {
                        out.println("checked");
                    }%>/>True<br/>
                <input type="radio" name="available" value="0" <% if (!available) {
                        out.println("checked");
                    }%>/>False<br/>
            </td>
        </tr>
        <tr>
            <td>
                <strong>Workload Ratio:</strong>
            </td>
            <td>
                <% out.println(rs.getDouble(MySQL.DB_LOGIN_WORKLOAD));%>
            </td>
        </tr>
        <tr>
            <td></td>
            <td><br/><input type="submit" value="Save Changes"/></td>
        </tr>
    </table>
</form>
<%        }
    } catch (Exception e) {
        System.out.println("Failed to establish connection to MySQL database.");
        e.printStackTrace();
    } finally {
        try {
            con.close();
            rs.close();
            ps.close();
        } catch (Exception e2) {
            e2.printStackTrace();
        }
    }
%>